package com.fengyuncx.tp.ali_crash_report;

import androidx.annotation.NonNull;

import com.alibaba.ha.adapter.AliHaAdapter;
import com.alibaba.ha.adapter.AliHaConfig;
import com.alibaba.ha.adapter.Plugin;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** AliCrashReportPlugin */
public class AliCrashReportPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  public static void init(AliHaConfig config) {
    AliHaAdapter.getInstance().addPlugin(Plugin.crashreporter);    //崩溃分析，如不需要可注释掉
//        AliHaAdapter.getInstance().addPlugin(Plugin.apm);              //性能监控，如不需要可注释掉
//    AliHaAdapter.getInstance().addPlugin(Plugin.tlog);             //移动日志，如不、需要可注释掉

    AliHaAdapter.getInstance().openDebug(true);          //调试日志开关
    AliHaAdapter.getInstance().start(config);
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "ali_crash_report");
    channel.setMethodCallHandler(this);
  }

  // This static function is optional and equivalent to onAttachedToEngine. It supports the old
  // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
  // plugin registration via this function while apps migrate to use the new Android APIs
  // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
  //
  // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
  // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
  // depending on the user's project. onAttachedToEngine or registerWith must both be defined
  // in the same class.
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "ali_crash_report");
    channel.setMethodCallHandler(new AliCrashReportPlugin());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if(call.method.equals("testCrash")){
      testCrash();
    } else if(call.method.equals("updateNick")){
      if(call.arguments != null && call.arguments instanceof String){
        String nick = (String) call.arguments;
        updateNick(nick);
      }
    } else {
      result.notImplemented();
    }
  }

  private void testCrash() {
    GlobalThreadManager.runInThreadPool(new Runnable() {
      @Override
      public void run() {
        String a = "aaa",b = null;
        System.out.println("AliCrashReportPlugin.testCrash b"+b.toLowerCase());
      }
    });
  }

  private void updateNick(String nick) {
    AliHaAdapter.getInstance().updateUserNick(nick);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
